<!doctype html>
<html class="no-js" lang="">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="x-ua-compatible" content="ie=edge">
        <title>sup-mimi-program-server documentation</title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <link rel="icon" type="image/x-icon" href="../images/favicon.ico">
	   <link rel="stylesheet" href="../styles/style.css">
        <link rel="stylesheet" href="../styles/dark.css">
        <link rel="stylesheet" href="../styles/postmark.css">
    </head>
    <body>
          <script>
               // Blocking script to avaoid flickering dark mode
               // Dark mode toggle button
               var useDark = window.matchMedia('(prefers-color-scheme: dark)');
               var darkModeState = useDark.matches;
               var $darkModeToggleSwitchers = document.querySelectorAll('.dark-mode-switch input');
               var $darkModeToggles = document.querySelectorAll('.dark-mode-switch');
               var darkModeStateLocal = localStorage.getItem('compodoc_darkmode-state');

               function checkToggle(check) {
                    for (var i = 0; i < $darkModeToggleSwitchers.length; i++) {
                         $darkModeToggleSwitchers[i].checked = check;
                    }
               }

               function toggleDarkMode(state) {
                    if (window.localStorage) {
                         localStorage.setItem('compodoc_darkmode-state', state);
                    }

                    checkToggle(state);

                    const hasClass = document.body.classList.contains('dark');

                    if (state) {
                         for (var i = 0; i < $darkModeToggles.length; i++) {
                              $darkModeToggles[i].classList.add('dark');
                         }
                         if (!hasClass) {
                              document.body.classList.add('dark');
                         }
                    } else {
                         for (var i = 0; i < $darkModeToggles.length; i++) {
                              $darkModeToggles[i].classList.remove('dark');
                         }
                         if (hasClass) {
                              document.body.classList.remove('dark');
                         }
                    }
               }

               useDark.addEventListener('change', function (evt) {
                    toggleDarkMode(evt.matches);
               });
               if (darkModeStateLocal) {
                    darkModeState = darkModeStateLocal === 'true';
               }
               toggleDarkMode(darkModeState);

               if ($darkModeToggles.length > 0) {
                    for (var i = 0; i < $darkModeToggleSwitchers.length; i++) {
                         $darkModeToggleSwitchers[i].addEventListener('change', function (event) {
                              darkModeState = !darkModeState;
                              toggleDarkMode(darkModeState);
                         });
                    }
               }
          </script>

        <div class="navbar navbar-default navbar-fixed-top d-block d-sm-none">
            <a href="../" class="navbar-brand">sup-mimi-program-server documentation</a>
            <button type="button" class="btn btn-default btn-menu ion-ios-menu" id="btn-menu"></button>
        </div>

        <div class="xs-menu menu" id="mobile-menu">
                <div id="book-search-input" role="search"><input type="text" placeholder="Type to search"></div>            <compodoc-menu></compodoc-menu>
        </div>

        <div class="container-fluid main">
           <div class="row main">
               <div class="hidden-xs menu">
                   <compodoc-menu mode="normal"></compodoc-menu>
               </div>
               <!-- START CONTENT -->
               <div class="content injectable">
                   <div class="content-data">








<ol class="breadcrumb">
  <li class="breadcrumb-item">Injectables</li>
  <li class="breadcrumb-item" >FeishuService</li>
</ol>

<ul class="nav nav-tabs" role="tablist">
        <li class="nav-item">
            <a href="#info" 
                class="nav-link"
                class="nav-link active"
                role="tab" id="info-tab" data-bs-toggle="tab" data-link="info">Info</a>
        </li>
        <li class="nav-item">
            <a href="#source" 
                class="nav-link"
                
                role="tab" id="source-tab" data-bs-toggle="tab" data-link="source">Source</a>
        </li>
</ul>

<div class="tab-content">
    <div class="tab-pane fade active in" id="info">
        <p class="comment">
            <h3>File</h3>
        </p>
        <p class="comment">
            <code>src/feishu/feishu.service.ts</code>
        </p>





            <section data-compodoc="block-index">
    <h3 id="index">Index</h3>
    <table class="table table-sm table-bordered index-table">
        <tbody>
                <tr>
                    <td class="col-md-4">
                        <h6><b>Properties</b></h6>
                    </td>
                </tr>
                <tr>
                    <td class="col-md-4">
                        <ul class="index-list">
                            <li>
                                    <span class="modifier"></span>
                                    <span class="modifier">Private</span>
                                <a href="#configService" >configService</a>
                            </li>
                        </ul>
                    </td>
                </tr>

                <tr>
                    <td class="col-md-4">
                        <h6><b>Methods</b></h6>
                    </td>
                </tr>
                <tr>
                    <td class="col-md-4">
                        <ul class="index-list">
                            <li>
                                    <span class="modifier">Async</span>
                                <a href="#addTenantAccessTokenToDb" >addTenantAccessTokenToDb</a>
                            </li>
                            <li>
                                    <span class="modifier">Async</span>
                                <a href="#getBatchOpenIdByPhone" >getBatchOpenIdByPhone</a>
                            </li>
                            <li>
                                <a href="#getCardMessagesDto" >getCardMessagesDto</a>
                            </li>
                            <li>
                                    <span class="modifier">Async</span>
                                <a href="#getHeadersRequestAccessToken" >getHeadersRequestAccessToken</a>
                            </li>
                            <li>
                                    <span class="modifier">Async</span>
                                <a href="#getTenantAccessTokenFromDB" >getTenantAccessTokenFromDB</a>
                            </li>
                            <li>
                                    <span class="modifier">Async</span>
                                <a href="#getTenantAccessTokenFromOrigin" >getTenantAccessTokenFromOrigin</a>
                            </li>
                            <li>
                                <a href="#getTextMessagesToUser" >getTextMessagesToUser</a>
                            </li>
                            <li>
                                    <span class="modifier">Async</span>
                                <a href="#sendMessagesToUser" >sendMessagesToUser</a>
                            </li>
                        </ul>
                    </td>
                </tr>





        </tbody>
    </table>
</section>

            <section data-compodoc="block-constructor">
    <h3 id="constructor">Constructor</h3>
        <table class="table table-sm table-bordered">
            <tbody>
                <tr>
                    <td class="col-md-4">
<code>constructor(tenantModel: ReturnModelType<>, httpService: HttpService)</code>
                    </td>
                </tr>
                        <tr>
                            <td class="col-md-4">
                                <div class="io-line">Defined in <a href="" data-line="31" class="link-to-prism">src/feishu/feishu.service.ts:31</a></div>
                            </td>
                        </tr>

                <tr>
                    <td class="col-md-4">
                            <div>
                                    <b>Parameters :</b>
                                    <table class="params">
                                        <thead>
                                            <tr>
                                                <td>Name</td>
                                                    <td>Type</td>
                                                <td>Optional</td>
                                            </tr>
                                        </thead>
                                        <tbody>
                                                <tr>
                                                        <td>tenantModel</td>
                                                  
                                                        <td>
                                                                    <code>ReturnModelType&lt;&gt;</code>
                                                        </td>
                                                  
                                                    <td>
                                                            No
                                                    </td>
                                                    
                                                </tr>
                                                <tr>
                                                        <td>httpService</td>
                                                  
                                                        <td>
                                                                    <code>HttpService</code>
                                                        </td>
                                                  
                                                    <td>
                                                            No
                                                    </td>
                                                    
                                                </tr>
                                        </tbody>
                                    </table>
                            </div>
                    </td>
                </tr>
            </tbody>
        </table>
</section>

            <section data-compodoc="block-methods">
    
    <h3 id="methods">
        Methods
    </h3>
    <table class="table table-sm table-bordered">
        <tbody>
            <tr>
                <td class="col-md-4">
                    <a name="addTenantAccessTokenToDb"></a>
                    <span class="name">
                            <span class="modifier">Async</span>
                        <span ><b>addTenantAccessTokenToDb</b></span>
                        <a href="#addTenantAccessTokenToDb"><span class="icon ion-ios-link"></span></a>
                    </span>
                </td>
            </tr>
            <tr>
                <td class="col-md-4">
                        <span class="modifier-icon icon ion-ios-reset"></span>
                    <code>addTenantAccessTokenToDb(createDto: <a href="../classes/CreateTenantAccessToken.html" target="_self">CreateTenantAccessToken</a>)</code>
                </td>
            </tr>


                    <tr>
                        <td class="col-md-4">
                            <div class="io-line">Defined in <a href="" data-line="84"
                                    class="link-to-prism">src/feishu/feishu.service.ts:84</a></div>
                        </td>
                    </tr>


            <tr>
                <td class="col-md-4">
                        <div class="io-description"><p>将租户访问令牌添加到数据库中</p>
</div>

                            <div class="io-description">
                                <b>Parameters :</b>
                                
                                <table class="params">
                                    <thead>
                                        <tr>
                                            <td>Name</td>
                                                <td>Type</td>
                                            <td>Optional</td>
                                                <td>Description</td>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr>
                                                <td>createDto</td>
                                            <td>
                                                            <code><a href="../classes/CreateTenantAccessToken.html" target="_self" >CreateTenantAccessToken</a></code>
                                            </td>

                                            <td>
                                                    No
                                            </td>


                                            <td>
                                                    <p>创建租户访问令牌的DTO对象</p>

                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        <div class="io-description">
                            <b>Returns : </b>    <code>unknown</code>

                        </div>
                            <div class="io-description">
                                <p>返回创建的租户访问令牌对象</p>

                            </div>
                </td>
            </tr>
        </tbody>
    </table>
    <table class="table table-sm table-bordered">
        <tbody>
            <tr>
                <td class="col-md-4">
                    <a name="getBatchOpenIdByPhone"></a>
                    <span class="name">
                            <span class="modifier">Async</span>
                        <span ><b>getBatchOpenIdByPhone</b></span>
                        <a href="#getBatchOpenIdByPhone"><span class="icon ion-ios-link"></span></a>
                    </span>
                </td>
            </tr>
            <tr>
                <td class="col-md-4">
                        <span class="modifier-icon icon ion-ios-reset"></span>
                    <code>getBatchOpenIdByPhone(dto: <a href="../classes/GetBatchOpenIdByPhoneDto.html" target="_self">GetBatchOpenIdByPhoneDto</a>)</code>
                </td>
            </tr>


                    <tr>
                        <td class="col-md-4">
                            <div class="io-line">Defined in <a href="" data-line="140"
                                    class="link-to-prism">src/feishu/feishu.service.ts:140</a></div>
                        </td>
                    </tr>


            <tr>
                <td class="col-md-4">
                        <div class="io-description"><p>通过手机号批量获取 OpenId</p>
</div>

                            <div class="io-description">
                                <b>Parameters :</b>
                                
                                <table class="params">
                                    <thead>
                                        <tr>
                                            <td>Name</td>
                                                <td>Type</td>
                                            <td>Optional</td>
                                                <td>Description</td>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr>
                                                <td>dto</td>
                                            <td>
                                                            <code><a href="../classes/GetBatchOpenIdByPhoneDto.html" target="_self" >GetBatchOpenIdByPhoneDto</a></code>
                                            </td>

                                            <td>
                                                    No
                                            </td>


                                            <td>
                                                    <p>包含租户信息和其它信息的对象</p>

                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        <div class="io-description">
                            <b>Returns : </b>    <code>unknown</code>

                        </div>
                            <div class="io-description">
                                <p>返回响应数据</p>

                            </div>
                </td>
            </tr>
        </tbody>
    </table>
    <table class="table table-sm table-bordered">
        <tbody>
            <tr>
                <td class="col-md-4">
                    <a name="getCardMessagesDto"></a>
                    <span class="name">
                        <span ><b>getCardMessagesDto</b></span>
                        <a href="#getCardMessagesDto"><span class="icon ion-ios-link"></span></a>
                    </span>
                </td>
            </tr>
            <tr>
                <td class="col-md-4">
<code>getCardMessagesDto(dto: <a href="../classes/SendMessagesDto.html" target="_self">SendMessagesDto</a>)</code>
                </td>
            </tr>


                    <tr>
                        <td class="col-md-4">
                            <div class="io-line">Defined in <a href="" data-line="180"
                                    class="link-to-prism">src/feishu/feishu.service.ts:180</a></div>
                        </td>
                    </tr>


            <tr>
                <td class="col-md-4">

                            <div class="io-description">
                                <b>Parameters :</b>
                                
                                <table class="params">
                                    <thead>
                                        <tr>
                                            <td>Name</td>
                                                <td>Type</td>
                                            <td>Optional</td>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr>
                                                <td>dto</td>
                                            <td>
                                                            <code><a href="../classes/SendMessagesDto.html" target="_self" >SendMessagesDto</a></code>
                                            </td>

                                            <td>
                                                    No
                                            </td>


                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        <div class="io-description">
                            <b>Returns : </b>        <code><a href="../classes/SendCardMessagesToUserDto.html" target="_self" >SendCardMessagesToUserDto</a></code>

                        </div>
                            <div class="io-description">
                                
                            </div>
                </td>
            </tr>
        </tbody>
    </table>
    <table class="table table-sm table-bordered">
        <tbody>
            <tr>
                <td class="col-md-4">
                    <a name="getHeadersRequestAccessToken"></a>
                    <span class="name">
                            <span class="modifier">Async</span>
                        <span ><b>getHeadersRequestAccessToken</b></span>
                        <a href="#getHeadersRequestAccessToken"><span class="icon ion-ios-link"></span></a>
                    </span>
                </td>
            </tr>
            <tr>
                <td class="col-md-4">
                        <span class="modifier-icon icon ion-ios-reset"></span>
                    <code>getHeadersRequestAccessToken(tenant: <a href="../undefineds/QueryFeiShuTenantAccessTokenType.html" target="_self">QueryFeiShuTenantAccessTokenType</a>)</code>
                </td>
            </tr>


                    <tr>
                        <td class="col-md-4">
                            <div class="io-line">Defined in <a href="" data-line="38"
                                    class="link-to-prism">src/feishu/feishu.service.ts:38</a></div>
                        </td>
                    </tr>


            <tr>
                <td class="col-md-4">

                            <div class="io-description">
                                <b>Parameters :</b>
                                
                                <table class="params">
                                    <thead>
                                        <tr>
                                            <td>Name</td>
                                                <td>Type</td>
                                            <td>Optional</td>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr>
                                                <td>tenant</td>
                                            <td>
                                                            <code><a href="../miscellaneous/typealiases.html#QueryFeiShuTenantAccessTokenType" target="_self" >QueryFeiShuTenantAccessTokenType</a></code>
                                            </td>

                                            <td>
                                                    No
                                            </td>


                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        <div class="io-description">
                            <b>Returns : </b>    <code>unknown</code>

                        </div>
                            <div class="io-description">
                                
                            </div>
                </td>
            </tr>
        </tbody>
    </table>
    <table class="table table-sm table-bordered">
        <tbody>
            <tr>
                <td class="col-md-4">
                    <a name="getTenantAccessTokenFromDB"></a>
                    <span class="name">
                            <span class="modifier">Async</span>
                        <span ><b>getTenantAccessTokenFromDB</b></span>
                        <a href="#getTenantAccessTokenFromDB"><span class="icon ion-ios-link"></span></a>
                    </span>
                </td>
            </tr>
            <tr>
                <td class="col-md-4">
                        <span class="modifier-icon icon ion-ios-reset"></span>
                    <code>getTenantAccessTokenFromDB(tenant: <a href="../undefineds/QueryFeiShuTenantAccessTokenType.html" target="_self">QueryFeiShuTenantAccessTokenType</a>)</code>
                </td>
            </tr>


                    <tr>
                        <td class="col-md-4">
                            <div class="io-line">Defined in <a href="" data-line="52"
                                    class="link-to-prism">src/feishu/feishu.service.ts:52</a></div>
                        </td>
                    </tr>


            <tr>
                <td class="col-md-4">
                        <div class="io-description"><p>从数据库获取租户访问令牌</p>
</div>

                            <div class="io-description">
                                <b>Parameters :</b>
                                
                                <table class="params">
                                    <thead>
                                        <tr>
                                            <td>Name</td>
                                                <td>Type</td>
                                            <td>Optional</td>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr>
                                                <td>tenant</td>
                                            <td>
                                                            <code><a href="../miscellaneous/typealiases.html#QueryFeiShuTenantAccessTokenType" target="_self" >QueryFeiShuTenantAccessTokenType</a></code>
                                            </td>

                                            <td>
                                                    No
                                            </td>


                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        <div class="io-description">
                            <b>Returns : </b>    <code>unknown</code>

                        </div>
                            <div class="io-description">
                                <p>返回租户访问令牌</p>

                            </div>
                </td>
            </tr>
        </tbody>
    </table>
    <table class="table table-sm table-bordered">
        <tbody>
            <tr>
                <td class="col-md-4">
                    <a name="getTenantAccessTokenFromOrigin"></a>
                    <span class="name">
                            <span class="modifier">Async</span>
                        <span ><b>getTenantAccessTokenFromOrigin</b></span>
                        <a href="#getTenantAccessTokenFromOrigin"><span class="icon ion-ios-link"></span></a>
                    </span>
                </td>
            </tr>
            <tr>
                <td class="col-md-4">
                        <span class="modifier-icon icon ion-ios-reset"></span>
                    <code>getTenantAccessTokenFromOrigin(tenant: <a href="../undefineds/QueryFeiShuTenantAccessTokenType.html" target="_self">QueryFeiShuTenantAccessTokenType</a>)</code>
                </td>
            </tr>


                    <tr>
                        <td class="col-md-4">
                            <div class="io-line">Defined in <a href="" data-line="100"
                                    class="link-to-prism">src/feishu/feishu.service.ts:100</a></div>
                        </td>
                    </tr>


            <tr>
                <td class="col-md-4">
                        <div class="io-description"><p>从原始来源获取租户访问令牌</p>
</div>

                            <div class="io-description">
                                <b>Parameters :</b>
                                
                                <table class="params">
                                    <thead>
                                        <tr>
                                            <td>Name</td>
                                                <td>Type</td>
                                            <td>Optional</td>
                                                <td>Description</td>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr>
                                                <td>tenant</td>
                                            <td>
                                                            <code><a href="../miscellaneous/typealiases.html#QueryFeiShuTenantAccessTokenType" target="_self" >QueryFeiShuTenantAccessTokenType</a></code>
                                            </td>

                                            <td>
                                                    No
                                            </td>


                                            <td>
                                                    <p>租户</p>

                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        <div class="io-description">
                            <b>Returns : </b>    <code>unknown</code>

                        </div>
                            <div class="io-description">
                                <p>返回获取到的租户访问令牌</p>

                            </div>
                </td>
            </tr>
        </tbody>
    </table>
    <table class="table table-sm table-bordered">
        <tbody>
            <tr>
                <td class="col-md-4">
                    <a name="getTextMessagesToUser"></a>
                    <span class="name">
                        <span ><b>getTextMessagesToUser</b></span>
                        <a href="#getTextMessagesToUser"><span class="icon ion-ios-link"></span></a>
                    </span>
                </td>
            </tr>
            <tr>
                <td class="col-md-4">
<code>getTextMessagesToUser(dto: <a href="../classes/SendMessagesToUserDto.html" target="_self">SendMessagesToUserDto</a>)</code>
                </td>
            </tr>


                    <tr>
                        <td class="col-md-4">
                            <div class="io-line">Defined in <a href="" data-line="172"
                                    class="link-to-prism">src/feishu/feishu.service.ts:172</a></div>
                        </td>
                    </tr>


            <tr>
                <td class="col-md-4">
                        <div class="io-description"><p>向用户发送消息</p>
</div>

                            <div class="io-description">
                                <b>Parameters :</b>
                                
                                <table class="params">
                                    <thead>
                                        <tr>
                                            <td>Name</td>
                                                <td>Type</td>
                                            <td>Optional</td>
                                                <td>Description</td>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr>
                                                <td>dto</td>
                                            <td>
                                                            <code><a href="../classes/SendMessagesToUserDto.html" target="_self" >SendMessagesToUserDto</a></code>
                                            </td>

                                            <td>
                                                    No
                                            </td>


                                            <td>
                                                    <p>发送消息的数据传输对象</p>

                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        <div class="io-description">
                            <b>Returns : </b>        <code><a href="../classes/SendMessagesToUserDto.html" target="_self" >SendMessagesToUserDto</a></code>

                        </div>
                            <div class="io-description">
                                <p>返回发送消息的结果</p>

                            </div>
                </td>
            </tr>
        </tbody>
    </table>
    <table class="table table-sm table-bordered">
        <tbody>
            <tr>
                <td class="col-md-4">
                    <a name="sendMessagesToUser"></a>
                    <span class="name">
                            <span class="modifier">Async</span>
                        <span ><b>sendMessagesToUser</b></span>
                        <a href="#sendMessagesToUser"><span class="icon ion-ios-link"></span></a>
                    </span>
                </td>
            </tr>
            <tr>
                <td class="col-md-4">
                        <span class="modifier-icon icon ion-ios-reset"></span>
                    <code>sendMessagesToUser(dto: <a href="../classes/SendMessagesDto.html" target="_self">SendMessagesDto</a>)</code>
                </td>
            </tr>


                    <tr>
                        <td class="col-md-4">
                            <div class="io-line">Defined in <a href="" data-line="187"
                                    class="link-to-prism">src/feishu/feishu.service.ts:187</a></div>
                        </td>
                    </tr>


            <tr>
                <td class="col-md-4">

                            <div class="io-description">
                                <b>Parameters :</b>
                                
                                <table class="params">
                                    <thead>
                                        <tr>
                                            <td>Name</td>
                                                <td>Type</td>
                                            <td>Optional</td>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr>
                                                <td>dto</td>
                                            <td>
                                                            <code><a href="../classes/SendMessagesDto.html" target="_self" >SendMessagesDto</a></code>
                                            </td>

                                            <td>
                                                    No
                                            </td>


                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        <div class="io-description">
                            <b>Returns : </b>    <code>unknown</code>

                        </div>
                            <div class="io-description">
                                
                            </div>
                </td>
            </tr>
        </tbody>
    </table>
</section>
            <section data-compodoc="block-properties">
    
    <h3 id="inputs">
        Properties
    </h3>
    <table class="table table-sm table-bordered">
        <tbody>
            <tr>
                <td class="col-md-4">
                    <a name="configService"></a>
                    <span class="name">
                            <span class="modifier"></span>
                            <span class="modifier">Private</span>
                        <span ><b>configService</b></span>
                        <a href="#configService"><span class="icon ion-ios-link"></span></a>
                    </span>
                </td>
            </tr>
                <tr>
                    <td class="col-md-4">
                        <i>Type : </i>    <code>ConfigService</code>

                    </td>
                </tr>
                <tr>
                    <td class="col-md-4">
                        <b>Decorators : </b>
                        <br />
                        <code>
                            @Inject(ConfigService)<br />
                        </code>
                    </td>
                </tr>
                    <tr>
                        <td class="col-md-4">
                                <div class="io-line">Defined in <a href="" data-line="31" class="link-to-prism">src/feishu/feishu.service.ts:31</a></div>
                        </td>
                    </tr>


        </tbody>
    </table>
</section>

    </div>


    <div class="tab-pane fade  tab-source-code" id="source">
        <pre class="line-numbers compodoc-sourcecode"><code class="language-typescript">import { Inject, Injectable } from &#x27;@nestjs/common&#x27;;
import { CreateFeishuDto } from &#x27;./dto/create-feishu.dto&#x27;;
import { UpdateFeishuDto } from &#x27;./dto/update-feishu.dto&#x27;;
import { CreateTenantAccessToken } from &#x27;./dto/create-tenant-access-token.dto&#x27;;
import { InjectModel } from &#x27;nestjs-typegoose&#x27;;
import { Feishu_Tenant as FeishuTenantSchema } from &#x27;./entities/feishu_tenant.entity&#x27;;
import { ReturnModelType } from &#x27;@typegoose/typegoose&#x27;;
import { isExpiredToUpdatedAtISOS } from &#x27;utils/common&#x27;;
import { QueryFeiShuTenantAccessTokenType } from &#x27;./dto/query-feishu-tenant-access-token.dto&#x27;;
import { HttpService } from &#x27;@nestjs/axios&#x27;;
import { ConfigService } from &#x27;@nestjs/config&#x27;;
import { firstValueFrom } from &#x27;rxjs&#x27;;
import { GetBatchOpenIdByPhoneDto } from &#x27;./dto/get-batch-openId-by-phone.dto&#x27;;
import {
  SendCardMessagesToUserDto,
  SendMessagesDto,
  SendMessagesToUserDto,
} from &#x27;./dto/send-messages-to-user.dto&#x27;;
import { temp_test_content } from &#x27;./store&#x27;;

@Injectable()
export class FeishuService {
  @Inject(ConfigService)
  private configService: ConfigService;

  constructor(
    @InjectModel(FeishuTenantSchema)
    private readonly tenantModel: ReturnModelType&lt;typeof FeishuTenantSchema&gt;,
    private readonly httpService: HttpService,
  ) {}
  async getHeadersRequestAccessToken(tenant: QueryFeiShuTenantAccessTokenType) {
    // 获取租户访问令牌
    const tenant_access_token_res &#x3D; await this.getTenantAccessTokenFromDB(
      tenant,
    );
    const { accessToken &#x3D; &#x27;&#x27; } &#x3D; tenant_access_token_res;
    return accessToken;
  }
  /**
   * 从数据库获取租户访问令牌
   *
   * @param appId 应用ID
   * @returns 返回租户访问令牌
   */
  async getTenantAccessTokenFromDB(tenant: QueryFeiShuTenantAccessTokenType) {
    const appId &#x3D; this.configService.get(&#x60;feishu.app.tenant.${tenant}.appId&#x60;);
    const res &#x3D; await this.tenantModel.findOne({ appId: appId });
    console.log(&#x27;从数据库获取 租户访问令牌 res&#x27;, res);
    if (res?._id) {
      // 有数据
      // 获取到之后，判断是否过期
      const { updatedAt, expire } &#x3D; res;
      // 获取当前时间
      const now &#x3D; new Date().toISOString();

      const isExpire &#x3D; isExpiredToUpdatedAtISOS(now, updatedAt, expire, 60);
      console.log(&#x27;isExpire&#x27;, isExpire);
      if (isExpire) {
        // 调用接口获取并且更新
        return this.getTenantAccessTokenFromOrigin(tenant);
      } else {
        console.log(&#x27;返回的 token 对象&#x27;, res);
        return res;
      }
    } else {
      // 没有数据：需要调用接口获取
      return this.getTenantAccessTokenFromOrigin(tenant);
    }
  }

  /**
   * 将租户访问令牌添加到数据库中
   *
   * @param createDto 创建租户访问令牌的DTO对象
   * @returns 返回创建的租户访问令牌对象
   */
  async addTenantAccessTokenToDb(createDto: CreateTenantAccessToken) {
    const { appId, ...rest } &#x3D; createDto;
    // 删除之前的数据，防止数据重复
    await this.tenantModel.deleteMany({
      appId,
    });

    return await this.tenantModel.create(createDto);
  }

  /**
   * 从原始来源获取租户访问令牌
   *
   * @param tenant 租户
   * @returns 返回获取到的租户访问令牌
   */
  async getTenantAccessTokenFromOrigin(
    tenant: QueryFeiShuTenantAccessTokenType,
  ) {
    const observable &#x3D; this.httpService.post(
      &#x60;${this.configService.get(
        &#x27;feishu.api.origin&#x27;,
      )}/open-apis/auth/v3/tenant_access_token/internal&#x60;,
      {
        app_id: this.configService.get(&#x60;feishu.app.tenant.${tenant}.appId&#x60;),
        app_secret: this.configService.get(
          &#x60;feishu.app.tenant.${tenant}.appSecret&#x60;,
        ),
      },
    );
    const res &#x3D; await firstValueFrom(observable);
    const {
      data,
      data: { code, expire, msg, tenant_access_token },
    } &#x3D; res;
    console.log(&#x27;getTenantAccessTokenFromOrigin data&#x27;, data);
    if (code &#x3D;&#x3D;&#x3D; 0) {
      const dto &#x3D; {
        appId: this.configService.get(&#x60;feishu.app.tenant.${tenant}.appId&#x60;),
        accessToken: tenant_access_token,
        expire: expire,
      };
      const data &#x3D; await this.addTenantAccessTokenToDb(dto);
      console.log(&#x27;从原始来源获取租户访问令牌 data&#x27;, data);
      return data;
    } else {
      return data;
    }
  }

  /**
   * 通过手机号批量获取 OpenId
   *
   * @param dto 包含租户信息和其它信息的对象
   * @returns 返回响应数据
   */
  async getBatchOpenIdByPhone(dto: GetBatchOpenIdByPhoneDto) {
    const { tenant, ...rest } &#x3D; dto;

    const accessToken &#x3D; await this.getHeadersRequestAccessToken(tenant);
    console.log(&#x27;accessToken&#x27;, accessToken);
    const headersRequest &#x3D; {
      &#x27;Content-Type&#x27;: &#x27;application/json&#x27;,
      Authorization: &#x60;Bearer ${accessToken}&#x60;,
    };
    console.log(&#x27;getBatchOpenIdByPhone accessToken&#x27;, accessToken);
    const observable &#x3D; this.httpService.post(
      &#x60;${this.configService.get(
        &#x27;feishu.api.origin&#x27;,
      )}/open-apis/contact/v3/users/batch_get_id?user_id_type&#x3D;open_id&#x60;,
      {
        ...rest,
      },
      {
        headers: headersRequest,
      },
    );
    const res &#x3D; await firstValueFrom(observable);
    console.log(&#x27;res?.data&#x27;, res?.data);
    return res?.data;
  }

  /**
   * 向用户发送消息
   *
   * @param dto 发送消息的数据传输对象
   * @returns 返回发送消息的结果
   */
  getTextMessagesToUser(dto: SendMessagesToUserDto) {
    const { content } &#x3D; dto as SendMessagesToUserDto;
    return {
      ...dto,
      content: JSON.stringify({ text: content }),
    } as SendMessagesToUserDto;
  }

  getCardMessagesDto(dto: SendMessagesDto) {
    return {
      ...dto,
      content: JSON.stringify(temp_test_content),
    } as SendCardMessagesToUserDto;
  }

  async sendMessagesToUser(dto: SendMessagesDto) {
    const { tenant, receivePhone, msg_type &#x3D; &#x27;text&#x27;, ...rest } &#x3D; dto;
    // 获取租户访问令牌
    const accessToken &#x3D; await this.getHeadersRequestAccessToken(tenant);
    console.log(&#x27;accessToken&#x27;, accessToken);
    const headersRequest &#x3D; {
      &#x27;Content-Type&#x27;: &#x27;application/json&#x27;,
      Authorization: &#x60;Bearer ${accessToken}&#x60;,
    };
    const receiveData &#x3D; await this.getBatchOpenIdByPhone({
      tenant,
      mobiles: [receivePhone],
    });
    console.log(&#x27;receiveData&#x27;, receiveData);
    const {
      code,
      data,
      data: { user_list &#x3D; [] },
    } &#x3D; receiveData;
    if (code !&#x3D;&#x3D; 0) {
      return receiveData;
    }
    const receive_id &#x3D; user_list.find(
      (item) &#x3D;&gt; item.mobile &#x3D;&#x3D;&#x3D; receivePhone,
    )?.user_id;
    let cDto: any &#x3D; {
      ...rest,
      msg_type,
      receive_id,
    };
    if (msg_type &#x3D;&#x3D;&#x3D; &#x27;text&#x27;) {
      cDto &#x3D; this.getTextMessagesToUser(cDto);
    } else if (msg_type &#x3D;&#x3D;&#x3D; &#x27;interactive&#x27;) {
      cDto &#x3D; this.getCardMessagesDto(cDto);
    }
    console.log(&#x27;cDto&#x27;, cDto);

    const observable &#x3D; this.httpService.post(
      &#x60;${this.configService.get(
        &#x27;feishu.api.origin&#x27;,
      )}/open-apis/im/v1/messages?receive_id_type&#x3D;open_id&#x60;,
      {
        ...cDto,
      },
      {
        headers: headersRequest,
      },
    );
    const res &#x3D; await firstValueFrom(observable);
    console.log(&#x27;res?.data&#x27;, res?.data);
    return res?.data;
  }
}
</code></pre>
    </div>

</div>













                   </div><div class="search-results">
    <div class="has-results">
        <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
        <ul class="search-results-list"></ul>
    </div>
    <div class="no-results">
        <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
    </div>
</div>
</div>
               <!-- END CONTENT -->
           </div>
       </div>

          <label class="dark-mode-switch">
               <input type="checkbox">
               <span class="slider">
                    <svg class="slider-icon" viewBox="0 0 24 24" fill="none" height="20" stroke="#000" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" width="20" xmlns="http://www.w3.org/2000/svg">
                    <path d="M21 12.79A9 9 0 1111.21 3 7 7 0 0021 12.79z"></path>
                    </svg>
               </span>
          </label>

       <script>
            var COMPODOC_CURRENT_PAGE_DEPTH = 1;
            var COMPODOC_CURRENT_PAGE_CONTEXT = 'injectable';
            var COMPODOC_CURRENT_PAGE_URL = 'FeishuService.html';
            var MAX_SEARCH_RESULTS = 15;
       </script>

       <script src="../js/libs/custom-elements.min.js"></script>
       <script src="../js/libs/lit-html.js"></script>

       <script src="../js/menu-wc.js" defer></script>
       <script nomodule src="../js/menu-wc_es5.js" defer></script>

       <script src="../js/libs/bootstrap-native.js"></script>

       <script src="../js/libs/es6-shim.min.js"></script>
       <script src="../js/libs/EventDispatcher.js"></script>
       <script src="../js/libs/promise.min.js"></script>
       <script src="../js/libs/zepto.min.js"></script>

       <script src="../js/compodoc.js"></script>

       <script src="../js/tabs.js"></script>
       <script src="../js/menu.js"></script>
       <script src="../js/libs/clipboard.min.js"></script>
       <script src="../js/libs/prism.js"></script>
       <script src="../js/sourceCode.js"></script>
          <script src="../js/search/search.js"></script>
          <script src="../js/search/lunr.min.js"></script>
          <script src="../js/search/search-lunr.js"></script>
          <script src="../js/search/search_index.js"></script>
       <script src="../js/lazy-load-graphs.js"></script>


    </body>
</html>
